package edu.pku.hawk.controller.detectmonitor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;

import edu.pku.hawk.controller.util.Config;
import edu.pku.hawk.controller.util.Constants;
import edu.pku.hawk.controller.util.DetectorInfo;
import edu.pku.hawk.controller.util.RegionPath;
import edu.pku.hawk.controller.util.RegionPoint;
import edu.pku.hawk.controller.util.ReportQueue;
import edu.pku.hawk.controller.util.SensorAllData;

public class AnalyzeResult {

	private DetectReport dtctRprt;
	// private Queue<DetectReport> dtctRprtList = new
	// LinkedList<DetectReport>();
	private HashMap<Integer, DetectReport> dtctRprtById = new HashMap<Integer, DetectReport>();
	private HashMap<Integer, ReportQueue> dtctRprtMap = new HashMap<Integer, ReportQueue>();

	private HashMap<Integer, Queue<Integer>> SensorTypePath = new HashMap<Integer, Queue<Integer>>();
	private RegionPath fusionPath = new RegionPath(Constants.TYPE_TRACK);
	private HashMap<Integer, RegionPath> singlePathSet = new HashMap<Integer, RegionPath>();
	private HashMap<Integer, SensorAllData> sensorAllDataMap = new HashMap<Integer, SensorAllData>();

	private DetectReport lastRFID = null;

	public DetectReport getLastRFID() {
		return lastRFID;
	}

	public void setLastRFID(DetectReport lastRFID) {
		this.lastRFID = lastRFID;
	}

	public AnalyzeResult() {
		this(null);
	}

	public AnalyzeResult(DetectReport report) {
		this.dtctRprt = report;
		Config config = Config.Singleton.Instance;
		ArrayList<DetectorInfo> dinfolist = config.getSersorInfoList();
		for (DetectorInfo info : dinfolist) {
			int did = Integer.valueOf(info.id);
			dtctRprtMap.put(did, new ReportQueue(Config.MonitorBuffer));
		}
	}

	public boolean addReport(DetectReport r) {
		// boolean bo1 = dtctRprtList.offer(r);
		int id = r.detectorId;
		ReportQueue queue = dtctRprtMap.get(id);
		if (queue.full()) {
			queue.dequeue();
		}
		dtctRprtById.put(r.detectorId, r);
		boolean bo2 = queue.enqueue(r);
		return bo2;
	}

	// public DetectReport pollReport() {
	// return dtctRprtList.poll();
	// }

	// public DetectReport[] getReports() {
	// DetectReport[] dr = new DetectReport[dtctRprtList.size()];
	// return (dtctRprtList.toArray(dr));
	// }

	// public void clearReportQueue() {
	// dtctRprtList.clear();
	// }

	public void clearReportMapById() {
		dtctRprtById.clear();
	}

	public void clearReportMapQueue() {
		Iterator<Map.Entry<Integer, ReportQueue>> it = dtctRprtMap.entrySet().iterator();
		while (it.hasNext()) {
			ReportQueue queue = it.next().getValue();
			queue.clear();
		}
	}

	public void clearPath() {
		fusionPath.clear();
		Iterator<Map.Entry<Integer, RegionPath>> it = singlePathSet.entrySet().iterator();
		while (it.hasNext()) {
			it.next().getValue().clear();
		}
	}

	public RegionPath getFusionPath() {
		return fusionPath;
	}

	public HashMap<Integer, RegionPath> getSinglePathSet() {
		return singlePathSet;
	}

	public void setFusionPath(RegionPath fusionPath) {
		this.fusionPath = fusionPath;
	}

	public void addFusionPoint(RegionPoint point) {
		this.fusionPath.add(point);
		if (point.objType == Constants.TYPE_TRACE_TANK) {
			this.fusionPath.objType = point.objType;
		} else {
			this.fusionPath.objType = Constants.TYPE_TRACE_TRUNK;
		}
	}

	public void addSinglePathPoint(RegionPoint point) {
		RegionPath singlePath = singlePathSet.get(point.sensorType);
		if (singlePath == null) {
			singlePath = new RegionPath(point.sensorType);
		}
		singlePath.add(point);
		if (point.objType == Constants.TYPE_TRACE_TANK) {
			singlePath.objType = point.objType;
		} else {
			singlePath.objType = Constants.TYPE_TRACE_TRUNK;
		}
		singlePathSet.put(point.sensorType, singlePath);
	}

	public DetectReport getDtctRprt() {
		return dtctRprt;
	}

	public void setDtctRprt(DetectReport dtctRprt) {
		this.dtctRprt = dtctRprt;
	}

	// public Queue<DetectReport> getDtctRprtList() {
	// return dtctRprtList;
	// }

	// public void setDtctRprtList(Queue<DetectReport> dtctRprtList) {
	// this.dtctRprtList = dtctRprtList;
	// }

	public HashMap<Integer, Queue<Integer>> getSensorTypePath() {
		return SensorTypePath;
	}

	public void addSensorPoint(int type, int sensorId) {
		Queue<Integer> queue = SensorTypePath.get(type);
		if (queue == null) {
			queue = new LinkedList<Integer>();
		}
		queue.offer(sensorId);
		SensorTypePath.put(type, queue);
	}

	public HashMap<Integer, ReportQueue> getDtctRprtMap() {
		return dtctRprtMap;
	}

	public HashMap<Integer, DetectReport> getDMap() {
		return dtctRprtById;
	}

	public HashMap<Integer, SensorAllData> getSensorAllDataMap() {
		return sensorAllDataMap;
	}

	public void setSensorAllDataMap(HashMap<Integer, SensorAllData> sensorAllDataMap) {
		this.sensorAllDataMap = sensorAllDataMap;
	}
}
